package exo1;

import java.io.*;
import java.util.*;

public class LectureEcritureVehicules {

	private static ArrayList<Vehicule> readVehicules(Scanner in)
	{
		ArrayList<Vehicule> vehicules = new ArrayList<Vehicule>();
		while(in.hasNextLine())
		{
			Vehicule v = new Vehicule();
			v.readData(in);
			vehicules.add(v);
		}
		return vehicules;
	}
	private static void writeVehicules(ArrayList<Vehicule> vehicules, PrintWriter out)
	{
		for(Vehicule temp : vehicules)
		{
			temp.writeData(out);
		}
	}
	
	public static void main(
			String[] args) {
		
		PrintWriter out = null;
		Scanner in = null;
		
		try {
			out = new PrintWriter("vehicules.txt");
			
			Vehicule v1 = new Vehicule(1,"Ford","Fiesta",2004,10000,5345,12);
			Vehicule v2 = new Vehicule(2,"Renault","Clio",2001,150000,2300,5);
			Vehicule v3 = new Vehicule(3,"Porsche","Carrera",2000,250000,50000,2);
			Vehicule v4 = new Vehicule(4,"Peugeot","208 GT",2012,120000,30000,5);
			
			v1.writeData(out);
			v2.writeData(out);
			v3.writeData(out);
			v4.writeData(out);
			
			BufferedInputStream bufferIn = new BufferedInputStream(new FileInputStream("vehicules.txt"));
			in = new Scanner(bufferIn);
			ArrayList<Vehicule> vehicules;
			vehicules = LectureEcritureVehicules.readVehicules(in);
			
			for(Vehicule temp : vehicules)
			{
				System.out.println(temp);
			}
			
			LectureEcritureVehicules.writeVehicules(vehicules, out);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("ko");
		}
		in.close();
		out.close();
	}

}
